using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace Awesome.Extensions.Reflection
{
    /// <summary>
    /// Provides type-safe reflection and come stack frame helpers.
    /// </summary>
    public static partial class Method
    {
      
        #region Current & Calling

        /// <summary>
        /// Returns the current method.
        /// </summary>
        public static MethodBase Current
        {
            [MethodImpl(MethodImplOptions.NoInlining)]
            get
            {
                // from NLog::LogManager::GetCurrentClassLogger()
#if SILVERLIGHT
                var frame = new StackTrace().GetFrame(1);
#else
                var frame = new StackFrame(1, false);
#endif

                return frame.GetMethod();
            }
        }


        /// <summary>
        /// Returns method, that made a call to current. 
        /// NOTE: you must add [MethodImpl(MethodImplOptions.NoInlining)] attribute to method that uses this property to avoid issues caused by JIT.
        /// Anyway, there is no warranties about this feature.
        /// Might be removed from stable release.
        /// </summary>
        public static MethodBase Calling
        {
            [MethodImpl(MethodImplOptions.NoInlining)]
            get
            {
                var frame = new StackFrame(2, false);
                return frame.GetMethod();
            }
        }

        #endregion

    }
}